<?php

namespace app\admin\controller;


use think\Db;
use util\OrderTime;

class Index extends Backend
{
    protected $noNeedAuth = 'index,map,order_time,order_address,notification,get_menus,get_auths,menu_count';

    //最外层的首页
    public function index()
    {
        //查询当前登录用户的所有菜单的第一个默认打开的菜单id
        /* $firstMenuId = $this->auth->getDefaultMenuIdFromMenus();
        $this->assign('firstMenuId', $firstMenuId); */
        $this->setSeo('后台管理');
        return $this->fetch();
    }

    /**
     * 选择地图定位
     *
     * @return void
     */
    public function map()
    {
        return $this->fetch();
    }


    /**
     * 控制台
     * @return \think\response\View
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/2 23:46
     * Note:
     */
    public function dashboard()
    {
        return $this->fetch();
    }



    /**
     * 清除系统缓存
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/1 10:35
     * Note:
     */
    public function clear_cache()
    {
        $type = input('param.type/s', 'all');
        switch ($type) {
            case 'all':
            case 'content':
                $path = env('runtime_path') . 'cache';
                remove_directory($path);
                if ($type == 'content') {
                    break;
                }
            case 'template':
                $path = env('runtime_path') . 'temp';
                remove_directory($path);
                if ($type == 'template') {
                    break;
                }
            case 'browser':
                //清除浏览器缓存，只能是测试环境或单独只清理浏览器缓存的情况
                if (config('app_debug') || $type == 'browser') {
                    $versionFile = env('config_path') . 'assets.php';
                    $version = config('assets.version');
                    $newVersion = floatval($version) + 0.01;
                    $str = <<<EOT
<?php
//这是前端资源文件的版本控制，便于更新浏览器的缓存
return [
'version' => $newVersion,
];
EOT;
                    file_put_contents($versionFile, $str);
                }
        }

        $this->writeAdminLog('清空缓存');
        $this->success('清除完成');
    }


    /**
     * 获取提示通知和设置已读
     * Author: LCY<409028735@qq.com>
     * Time: 2021/11/30 19:49
     * Note:
     */
    public function notification()
    {
        if ($this->request->isPost()) {
            $group = input('group/s', '');
            $map[] = ['is_read', '=', 0];
            $map[] = ['admin_id', 'in', [0, $this->auth->id]];
            if (!empty($group)) {
                $map[] = ['group', '=', $group];
            }
            Db::name('admin_notification')->where($map)->setField('is_read', 1);
            $this->writeAdminLog('设置通知已读');
            $this->success('设置完成');
        } else {
            //案例参考
            /*$datas = [
                'total' => 10,
                //数据项，设置4组以内的数据, level:提示级别（notice、warning、error）,url:查看链接，content：内容，create_time:发送提示的时间
                'tabs' => [
                    'tab1' => [
                        'title' => '消息通知', //标题
                        'icon' => '', //图标
                        'list' => [
                            [
                                'level'   => 'notice',
                                'url'     => '',
                                'content' => '您的余额不足！',
                                'create_time' => '1小时前',
                            ],
                            [
                                'level'   => 'warning',
                                'url'     => '',
                                'content' => '您的余额不足2！',
                                'create_time' => '2小时前',
                            ],
                        ], //数据列表
                        'url' => 'sdf', //查看链接
                    ],
                    'tab2' => [
                        'title' => '审核通知', //标题
                        'icon' => '', //图标
                        'list' => [
                            [
                                'level'   => 'notice',
                                'url'     => '',
                                'content' => '提现待审核',
                                'create_time' => '10小时前',
                            ],
                            [
                                'level'   => 'warning',
                                'url'     => '',
                                'content' => '审核失败啦啊打发是的发送到发送到发打发是的发送到发送到发',
                                'create_time' => '2小时前',
                            ],
                        ], //数据列表
                        'url' => 'gh', //查看链接
                    ],
                    'tab3' => [
                        'title' => '日志通知', //标题
                        'icon' => '', //图标
                        'list' => [], //数据列表
                        'url' => '', //查看链接
                    ],
                ],

            ];*/
            //查询系统通知
            $groups = [
                'default' => [
                    'title' => '默认通知',
                    'icon'  =>  'bi bi-chat-square-text',
                    'list'  => [],
                    'url'   => '',
                    'total' => 0
                ],
                'default2' => [
                    'title' => '审核通知',
                    'icon'  =>  'bi bi-bell',
                    'list'  => [],
                    'url'   => '',
                    'total' => 0
                ],
            ];
            $total = 0;

            //开启消息通知才去查询
            if ($this->notificationTimer) {
                foreach ($groups as $k => $group) {
                    $datas = Db::name('admin_notification')->where('is_read', 0)->where('admin_id', 'in', [0, $this->auth->id])->order(['id' => 'desc'])->where('group', $k)->limit(100)->select();
                    $count = !empty($datas) ? count($datas) : 0;
                    $total += $count;
                    $groups[$k]['total'] = $count;
                    $groups[$k]['list'] = $datas;
                }
            }

            $datas = [
                'total' => $total,
                //数据项，设置4组以内的数据, level:提示级别（notice、warning、error）,url:查看链接，content：内容，create_time:发送提示的时间
                'tabs' => $groups,
            ];
            $this->success('ok', '', $datas);
        }
    }

    /**
     * 加载某个菜单的统计数据
     *
     * @return void
     */
    public function menu_count()
    {
        $keys = input('param.keys/s', '');
        $keys = explode(',', $keys);
        $keys = array_unique($keys);
        if (empty($keys)) {
            return ajax_return(1, 'ok', null);
        }

        $datas = [];
        //TODO 根据对应的key进行处理（获取统计数据或提示信息）

        //$datas = ['dashboard'=>'待审'];
        $datas = [];
        if (!empty($keys)) {
            foreach ($keys as $key) {
                if ($key == 'feedback') {
                    $datas[$key] = Db::name('feedback')->where('status', 0)->count();
                } 
            }
        }


        return ajax_return(1, 'ok', null, $datas);
    }

    /**
     * 图标查看
     * @return mixed
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/1 13:31
     * Note:
     */
    public function icon()
    {
        return $this->fetch('icon');
    }

    /**
     * 获取用户的菜单权限
     *
     * @return void
     */
    public function get_menus()
    {
        //sleep(3);
        $menus = $this->auth->getFrontendMemus();
        $this->success('ok', '', ['menus' => $menus]);
    }
}
